package com.bole.controller;

import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.dto.user.LeaveMessageDto;
import com.bole.dto.user.UserDto;
import com.bole.feign.reception.LeaveMessageFeign;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 留言
 */
@CrossOrigin
@RestController
@RequestMapping("leaveMessage")
public class LeaveMessageController {

    @Autowired
    private LeaveMessageFeign leaveMessageFeign;

    /**
     * 获取
     * @return
     */
    @GetMapping
    public Result getMessage() {
        UserDto user = (UserDto) SecurityUtils.getSubject().getPrincipal();
        return leaveMessageFeign.getMessageByUserId(user.getId());
    }

    /**
     * 回复 保存
     * @param leaveMessageDto
     * @return
     */
    @PostMapping
    public Result saveLeaveMessage(@RequestBody LeaveMessageDto leaveMessageDto){
        UserDto user = (UserDto) SecurityUtils.getSubject().getPrincipal();
        leaveMessageDto.setSellUserId(user.getId());
        leaveMessageDto.setMessageTime(new Date());
        return leaveMessageFeign.saveLeavingMessage(leaveMessageDto);
    }

}
